home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / file-tra / fsp-2.7 / fsp-2 / fsp / clients / fducmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-19  |  2.9 KB  |  111 lines

  1. /******************************************************************************
  2. * This file is Copyright 1992 by Philip G. Richards.  All Rights Reserved.
  3. * See the file README that came with this distribution for permissions on
  4. * code usage, copying, and distribution.  It comes with absolutely no warranty.
  5. * email: <pgr@prg.ox.ac.uk>
  6. ******************************************************************************/
  7.  
  8. /*****************************************************************************
  9. * reprogrammed as a stand alone client by Michael Meskes
  10. * <meskes@ulysses.informatik.rwth-aachen.de>
  11. ******************************************************************************/
  12.  
  13. #include "tweak.h"
  14. #include "client_def.h"
  15. #include "c_extern.h"
  16. #include "bsd_extern.h"
  17. #include "my-string.h"
  18.  
  19. extern int optind;
  20.  
  21. u_long total_file_size;
  22.  
  23. static void add_file_size PROTO4(char *, name, struct stat *, sbufp,
  24.                  int, mode, int,  level)
  25. {
  26.   register u_long file_size;
  27.   
  28.   file_size = (sbufp->st_size + 1023) / 1024;
  29.   total_file_size += file_size;
  30.   if(((mode & EACH) && (mode & RECURSIVE)) ||
  31.      ((mode & EACH) && level < 2)) printf("%-7d %s\n", file_size, name);
  32. }
  33.  
  34. static int start_dir PROTO3(char *, name, struct stat *, sbufp, u_long *, sum)
  35. {
  36.   *sum = total_file_size;
  37.   return(0);
  38. }
  39.  
  40. static void end_dir PROTO4(char *, path, int, mode, u_long, sum, int, level)
  41. {
  42.   /* directories are printed as default */
  43.   /* but, check recursion level */
  44.   if(((mode & RECURSIVE) && !(mode & SUM)) ||
  45.      ((level == 1) && (!(mode & SUM) || (mode & EACH))) ||
  46.      !level) {
  47.     sum = total_file_size - sum; /* this is the real value */
  48.     printf("%-7d %s\n", sum, path);
  49.   }
  50. }
  51.  
  52. /* ARGSUSED */
  53. int main PROTO3(int, argc, char **, argv, char **, envp)
  54. {
  55.   int mode=0;
  56.   int filcnt = 0;
  57.   static char *wild[2] = { ".", 0 };
  58.   char **files, *singlefile[2];
  59.   int optletter;
  60.   
  61.   env_client();
  62.   
  63.   while ((optletter=getopt(argc, argv,"ras")) != EOF)
  64.     switch (optletter) {
  65.       case 'r':
  66.         mode |= RECURSIVE; /* recursively read all subdirectories */
  67.     break;
  68.       case 's':
  69.     mode |= SUM; /* print sums only */
  70.     break;
  71.       case 'a':
  72.     mode |= EACH; /* print an entry for each file */
  73.     break;
  74.       default:
  75.     fprintf(stderr,"Usage: du [-r|a|s] directory name.\n");
  76.     exit(0);
  77.     }
  78.   
  79.   /* special case `du' without file arguments -- becomes `du .' */
  80.   if (argc == optind) {
  81.     argv=wild;
  82.     optind=0;
  83.   }
  84.   
  85.   for ( ; argv[optind]; optind++) {
  86.     if (!(files = glob(argv[optind]))) {
  87.       files = singlefile;
  88.       singlefile[0] = argv[optind];
  89.       singlefile[1] = 0;
  90.     }
  91.       
  92.     for ( ; *files; files++) {
  93.       util_process_file(*files, mode, add_file_size, start_dir, end_dir, 0);
  94.       filcnt++;
  95.     }
  96.   }
  97.   
  98.   if (filcnt > 1) {
  99.     fprintf(stdout, "--------:------\n");
  100.     fprintf(stdout, "%-7d   TOTAL\n", total_file_size);
  101.   }
  102.   
  103.   client_done();
  104.  
  105. #ifdef VMS
  106.   return 1;
  107. #else  
  108.   return 0;
  109. #endif
  110. }
  111.